package com.lcz.authority.common.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lcz.authority.common.annotation.Excel;
import com.zhou.starter.jpacomment.annotation.ColumnComment;
import com.zhou.starter.jpacomment.annotation.TableComment;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lcz
 * @desc 日志实体类
 * @mail lcz_0130@163.com
 * @date 2024/05/06
 */
@Data
@TableName("sys_log_oper")
@Table(name = "sys_log_oper")
@Entity
@TableComment("操作日志记录表")
public class SysOperLogEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Integer PAGE_NUM = 1;
    private static final Integer PAGE_SIZE = 10;
    /**
     * 日志主键
     */
    @Id
    @TableId(type = IdType.ASSIGN_ID)
    @ColumnComment("主键id")
    private String operId;
    /**
     * 服务名称，一般为spring.application.name
     */
    @Excel(name = "服务名称")
    @ColumnComment("服务名称")
    private String appName;
    /**
     * 操作模块
     */
    @Excel(name = "操作模块")
    @ColumnComment("操作模块")
    private String logName;
    /**
     * 日志记录内容
     */
    @Excel(name = "日志记录内容")
    @ColumnComment("日志记录内容")
    private String logContent;
    /**
     * 请求方法
     */
    @Excel(name = "请求方法")
    @ColumnComment("请求方法")
    private String method;
    /**
     * 请求方式
     */
    @Excel(name = "请求方式")
    @ColumnComment("请求方式")
    private String requestMethod;
    /**
     * 操作人员 id
     */
    @ColumnComment("操作人员 id")
    private String operUserId;
    /**
     * 请求url
     */
    @Excel(name = "请求url")
    @ColumnComment("请求url")
    private String operUrl;
    /**
     * 操作地址
     */
    @Excel(name = "操作地址")
    @ColumnComment("操作地址")
    private String operIp;
    /**
     * 请求参数
     */
    @Excel(name = "请求参数")
    @ColumnComment("请求参数")
    @Column(length = 2000)
    private String operParam;
    /**
     * 返回参数
     */
    @Excel(name = "返回参数")
    @ColumnComment("返回参数")
    @Column(length = 2000)
    private String jsonResult;
    /**
     * 操作状态（0正常 1异常）
     */
    @Excel(name = "状态", readConverterExp = "0=正常,1=异常")
    @ColumnComment("操作状态（0正常 1异常）")
    private Integer status;
    /**
     * 错误消息
     */
    @Excel(name = "错误消息")
    @ColumnComment("错误消息")
    @Column(length = 2000)
    private String errorMsg;
    /**
     * 操作时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Excel(name = "操作时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    @ColumnComment("操作时间")
    private Date operTime;

    @TableField(exist = false)
    @Transient
    private Map<String, Object> params;

    @NotNull(message = "页码不能为空")
    @Min(value = 1, message = "页码最小值为 1")
    @TableField(exist = false)
    @JsonIgnore
    @Transient
    private Integer pageNum = PAGE_NUM;

    @NotNull(message = "每页条数不能为空")
    @Min(value = 1, message = "每页条数最小值为 1")
    @Max(value = 100, message = "每页条数最大值为 100")
    @TableField(exist = false)
    @JsonIgnore
    @Transient
    private Integer pageSize = PAGE_SIZE;

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }
}
